package com.mmp.main.entity;

import java.io.Serializable;
import java.util.Date;
import java.util.List;

import javax.persistence.CascadeType;
import javax.persistence.Column;
import javax.persistence.Entity;
import javax.persistence.GeneratedValue;
import javax.persistence.GenerationType;
import javax.persistence.Id;
import javax.persistence.JoinColumn;
import javax.persistence.ManyToOne;
import javax.persistence.NamedQuery;
import javax.persistence.OneToMany;
import javax.persistence.Table;
import javax.persistence.Temporal;
import javax.persistence.TemporalType;

import org.hibernate.annotations.LazyCollection;
import org.hibernate.annotations.LazyCollectionOption;

import com.mmp.common.util.SearchRequired;

/**
 * The persistent class for the job_order database table.
 *
 */
@Entity
@Table(name = "job_order")
@NamedQuery(name = "JobOrder.findAll", query = "SELECT j FROM JobOrder j")
public class JobOrder implements Serializable {
	private static final long serialVersionUID = 1L;

	@Id
	@GeneratedValue(strategy = GenerationType.IDENTITY)
	@Column(name = "id")
	private Integer id;

	@Column(name = "agent_report_url")
	private String agentReportUrl;

	@Column(name = "cancel_reason")
	private String cancelReason;

	@SearchRequired
	private Boolean cancelled;

	@Temporal(TemporalType.TIMESTAMP)
	@Column(name = "cancelled_in")
	private Date cancelledIn;

	@SearchRequired
	private Boolean closed;

	@Temporal(TemporalType.TIMESTAMP)
	@Column(name = "closed_in")
	private Date closedIn;

	@Temporal(TemporalType.TIMESTAMP)
	@Column(name = "damage_date")
	private Date damageDate;

	@Column(name = "damage_description")
	private String damageDescription;

	@Column(name = "final_action")
	private String finalAction;

	@Temporal(TemporalType.TIMESTAMP)
	@Column(name = "final_action_date")
	private Date finalActionDate;

	@Column(name = "final_report_url")
	private String finalReportUrl;

	@Column(name = "first_action")
	private String firstAction;

	@Temporal(TemporalType.TIMESTAMP)
	@Column(name = "first_action_date")
	private Date firstActionDate;

	@Column(name = "fix_including_spare_parts")
	private Boolean fixIncludingSpareParts;

	@Temporal(TemporalType.TIMESTAMP)
	@Column(name = "job_order_date")
	private Date jobOrderDate;

	@Column(name = "job_order_no")
	@SearchRequired
	private String jobOrderNo;

	@Column(name = "second_action")
	private String secondAction;

	@Temporal(TemporalType.TIMESTAMP)
	@Column(name = "second_action_date")
	private Date secondActionDate;

	// bi-directional many-to-one asociation to HospitalDevice
	@ManyToOne
	@JoinColumn(name = "device_id")
	@SearchRequired(isObject = true, fields = { "serialNo", "code" })
	private HospitalDevice hospitalDevice;

	// bi-directional many-to-one association to User
	@ManyToOne
	@JoinColumn(name = "cancelled_by")
	private User cancelledBy;

	// bi-directional many-to-one association to User
	@ManyToOne
	@JoinColumn(name = "closed_by")
	private User closedBy;

	@ManyToOne
	@JoinColumn(name = "opened_by")
	private User openedBy;

	@Temporal(TemporalType.TIMESTAMP)
	@Column(name = "opened_in")
	private Date openedIn;

	// bi-directional many-to-one association to JobOrderSparePart
	@LazyCollection(LazyCollectionOption.FALSE)
	@OneToMany(cascade = CascadeType.ALL, mappedBy = "jobOrder", orphanRemoval = true)
	private List<JobOrderSparePart> jobOrderSpareParts;

	public JobOrder() {
	}

	public Integer getId() {
		return this.id;
	}

	public void setId(Integer id) {
		this.id = id;
	}

	public String getAgentReportUrl() {
		return this.agentReportUrl;
	}

	public void setAgentReportUrl(String agentReportUrl) {
		this.agentReportUrl = agentReportUrl;
	}

	public String getCancelReason() {
		return this.cancelReason;
	}

	public void setCancelReason(String cancelReason) {
		this.cancelReason = cancelReason;
	}

	public Boolean getCancelled() {
		return this.cancelled;
	}

	public void setCancelled(Boolean cancelled) {
		this.cancelled = cancelled;
	}

	public Date getCancelledIn() {
		return this.cancelledIn;
	}

	public void setCancelledIn(Date cancelledIn) {
		this.cancelledIn = cancelledIn;
	}

	public Date getOpenedIn() {
		return this.openedIn;
	}

	public void setOpenedIn(Date openedIn) {
		this.openedIn = openedIn;
	}

	public Boolean getClosed() {
		return this.closed;
	}

	public void setClosed(Boolean closed) {
		this.closed = closed;
	}

	public Date getClosedIn() {
		return this.closedIn;
	}

	public void setClosedIn(Date closedIn) {
		this.closedIn = closedIn;
	}

	public Date getDamageDate() {
		return this.damageDate;
	}

	public void setDamageDate(Date damageDate) {
		this.damageDate = damageDate;
	}

	public String getDamageDescription() {
		return this.damageDescription;
	}

	public void setDamageDescription(String damageDescription) {
		this.damageDescription = damageDescription;
	}

	public String getFinalAction() {
		return this.finalAction;
	}

	public void setFinalAction(String finalAction) {
		this.finalAction = finalAction;
	}

	public Date getFinalActionDate() {
		return this.finalActionDate;
	}

	public void setFinalActionDate(Date finalActionDate) {
		this.finalActionDate = finalActionDate;
	}

	public String getFinalReportUrl() {
		return this.finalReportUrl;
	}

	public void setFinalReportUrl(String finalReportUrl) {
		this.finalReportUrl = finalReportUrl;
	}

	public String getFirstAction() {
		return this.firstAction;
	}

	public void setFirstAction(String firstAction) {
		this.firstAction = firstAction;
	}

	public Date getFirstActionDate() {
		return this.firstActionDate;
	}

	public void setFirstActionDate(Date firstActionDate) {
		this.firstActionDate = firstActionDate;
	}

	public Boolean getFixIncludingSpareParts() {
		return this.fixIncludingSpareParts;
	}

	public void setFixIncludingSpareParts(Boolean fixIncludingSpareParts) {
		this.fixIncludingSpareParts = fixIncludingSpareParts;
	}

	public Date getJobOrderDate() {
		return this.jobOrderDate;
	}

	public void setJobOrderDate(Date jobOrderDate) {
		this.jobOrderDate = jobOrderDate;
	}

	public String getJobOrderNo() {
		return this.jobOrderNo;
	}

	public void setJobOrderNo(String jobOrderNo) {
		this.jobOrderNo = jobOrderNo;
	}

	public String getSecondAction() {
		return this.secondAction;
	}

	public void setSecondAction(String secondAction) {
		this.secondAction = secondAction;
	}

	public Date getSecondActionDate() {
		return this.secondActionDate;
	}

	public void setSecondActionDate(Date secondActionDate) {
		this.secondActionDate = secondActionDate;
	}

	public HospitalDevice getHospitalDevice() {
		return this.hospitalDevice;
	}

	public void setHospitalDevice(HospitalDevice hospitalDevice) {
		this.hospitalDevice = hospitalDevice;
	}

	public User getCancelledBy() {
		return this.cancelledBy;
	}

	public void setCancelledBy(User user1) {
		this.cancelledBy = user1;
	}

	public User getOpenedBy() {
		return this.openedBy;
	}

	public void setOpenedBy(User user) {
		this.openedBy = user;
	}

	public User getClosedBy() {
		return this.closedBy;
	}

	public void setClosedBy(User user2) {
		this.closedBy = user2;
	}

	public List<JobOrderSparePart> getJobOrderSpareParts() {
		return this.jobOrderSpareParts;
	}

	public void setJobOrderSpareParts(List<JobOrderSparePart> jobOrderSpareParts) {
		this.jobOrderSpareParts = jobOrderSpareParts;
	}

	public JobOrderSparePart addJobOrderSparePart(JobOrderSparePart jobOrderSparePart) {
		getJobOrderSpareParts().add(jobOrderSparePart);
		jobOrderSparePart.setJobOrder(this);

		return jobOrderSparePart;
	}

	public JobOrderSparePart removeJobOrderSparePart(JobOrderSparePart jobOrderSparePart) {
		getJobOrderSpareParts().remove(jobOrderSparePart);
		jobOrderSparePart.setJobOrder(null);

		return jobOrderSparePart;
	}

	public float getSparePartsTotalCost() {
		float sum = 0;
		if (jobOrderSpareParts != null)
			for (JobOrderSparePart p : jobOrderSpareParts)
				sum += p.getTotalPrice();
		return sum;
	}

}
